<?php

namespace app\api\controller;

use app\admin\model\shop\Article;
use app\admin\model\shop\Banner;
use app\admin\model\shop\Manystoretaps;
use app\admin\model\shop\Setmeal;
use app\admin\model\shop\Shoplist;
use app\common\controller\Api;
use app\common\model\Category;
use app\manystore\model\Manystore;
use app\manystore\model\ManystoreShop;


/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 获取首页全部
     *
     * @ApiTitle    (获取首页全部)
     * @ApiSummary  (获取首页全部)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/indexAll)
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="string", required=true, sample="数据")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function indexAll(){
        //分类
        $class = Category::where('type','=','shoplist')
            ->field('id,pid,type,name,image,flag')
            ->select();

        //轮播
        $banner = Banner::where('status','=',1)->select();

        foreach ($banner as $k=>$v){
            $banner[$k]['image']  = get_img_domain($v['image'],false);
        }
        //商品列表
        $shopList =[];
        foreach ($class as $key => $values) {
            $class[$key]['image'] = get_img($values['image']);
            $shop = Shoplist::where('category_id', '=', $values['id'])->order('id desc')->limit(5)->select();
            foreach ($shop as $k => $v) {
                $v['manystoretaps_text'] = Manystoretaps::where('id', 'in', $v['manystoretaps_ids'])->cache(60)->select();
                $value = explode(',', $v['smallimages']);
                foreach ($value as $k1 => $v1) {
                    $value[$k1] = get_img_domain($v1,false);//request()->domain() . $v1;

                }
                $shop[$k]['smallimages'] = implode($value, ',');
                //评论头像
                $article = Article::where('shoplist_id', '=', $v['id'])
                    ->alias('a')
                    ->join('fa_user w', 'a.user_id = w.id')
                    ->limit(5)
                    ->field('w.avatar')
                    ->cache(60)
                    ->select();
                foreach ($article as $k1 => $v1) {
                    if (empty($v1['avatar'])) {
                        $article[$k1]['avatar'] = request()->domain() . '/assets/img/avatar.png';
                    }
                }
                $shop[$k]['avatar'] = $article;
            }
            $shopList[$key]['className'] = $values['name'];
            $shopList[$key]['shopList'] = $shop;
        }


        //商品评价
        $article = Article::alias('a')
            ->join('user w','a.user_id = w.id')
            ->order('a.id desc')
            ->limit(20)
            ->field('a.id,a.shoplist_id,a.user_id,a.title,a.likenum,a.createtime,a.smallimages,a.content,w.nickname,w.avatar')
            ->select();
         //补全URL地址
        foreach ($article as $k=>$v){
            $value = explode(',', $v['smallimages']);
            foreach ($value as $k1 => $v1) {
                $value[$k1] = get_img_domain($v1,false);
            }
            $article[$k]['smallimages'] = implode($value, ',');
            if (!$v['avatar']) {
                //如果不需要启用首字母头像，请使用
                $article[$k]['avatar'] = '/assets/img/avatar.png';
            }
            $article[$k]['avatar'] = get_img_domain($article[$k]['avatar'],false);
        }

        $data['class'] = $class;
        $data['banner'] = $banner;
        $data['shopList'] = $shopList;
        $data['article'] = $article;
        return $this->success('获取成功',$data);
    }





    /**
     * 获取商品列表
     *
     * @ApiTitle    (获取商品列表)
     * @ApiSummary  (获取商品列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/index)
     * @ApiParams  (name=category_id, type=int, required=false, description="分类id")
     * @ApiParams   (name="page", type="integer", required=true, description="分页")
     * @ApiParams   (name="limit", type="string", required=true, description="limit")
     * @ApiParams   (name="title", type="string", required=false, description="搜索内容")
     * @ApiParams   (name="status", type="int", required=true, description="1正常2推荐")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="string", required=true, sample="数据")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function index()
    {
        $category_id = $this->request->request("category_id/d");
        $page = $this->request->request("page",1);
        $limit = $this->request->request("limit",10);
        $status = $this->request->request("status");
        $title = $this->request->request("title");
        $where=[];
        $where['status']=['<>',0];
        if ($category_id){
            $where['category_id']=['=',$category_id];
        }
        if ($title){
            $where['title']=['like','%'.$title.'%'];
        }
        if ($status){
            $where['status']=['=',$status];
        }
        $shopList = Shoplist::where($where)
            ->page($page,$limit)
            ->field('id,title,category_id,price,original_price,price_title,smallimages,shoplike,recommend,manystoretaps_ids')
            ->select();
        if (count($shopList) == 0){
            $this->error(__('没有更多内容了!'));
        }

        foreach ($shopList as $k => $v) {
            $v['manystoretaps_text'] = Manystoretaps::where('id','in',$v['manystoretaps_ids'])->cache(600)->select();
            $value = explode(',', $v['smallimages']);
            foreach ($value as $k1 => $v1) {
                $value[$k1] = get_img_domain($v1,false);//request()->domain() . $v1;

            }
            $shopList[$k]['smallimages'] = implode($value, ',');
            //评论头像
            $article  =  Article::where('shoplist_id','=',$v['id'])
                ->alias('a')
                ->join('fa_user w','a.user_id = w.id')
                ->limit(5)
                ->field('w.avatar')
                ->cache(600)
                ->select();
            foreach ($article as $k1=>$v1){
                if (empty($v1['avatar'])){
                    $article[$k1]['avatar'] =  request()->domain().'/assets/img/avatar.png';
                }
            }
            $shopList[$k]['avatar'] = $article;
        }
        $this->success('获取成功',$shopList);
    }
    /**
     * 获取商品详情
     *
     * @ApiTitle    (获取商品详情)
     * @ApiSummary  (获取商品详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/getShop)
     * @ApiParams  (name=shoplist_id, type=int, required=false, description="商品id")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="string", required=true, sample="数据")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function getShop(){
        $shoplist_id = $this->request->request("shoplist_id/d");
        $shop = Shoplist::where('id','=',$shoplist_id)
            ->field('*')
            ->find();
        if (!$shop){
            $this->error(__('没有更多内容了!'));
        }

        $value = explode(',', $shop['smallimages']);
        foreach ($value as $k1 => $v1) {
            $value[$k1] = request()->domain() . $v1;
        }
        $shop['smallimages'] = implode($value, ',');
        if ($shop['attachfile']){
            $shop['attachfile'] = get_img_domain($shop['attachfile'],false);
        }
        $setmeal = Setmeal::where('id','in',$shop['setmeal_ids'])->where('status','=',1)->field('id,name,configjson')->select();
        foreach ($setmeal as $k=>$v){
            $configjson_array=[];
            $configjson = json_decode($v['configjson'],true);
            unset($setmeal[$k]['configjson']);
            foreach ($configjson as $k1=>$v1){
                $configjson_array[] = ['name'=>$k1,'price'=>$v1];
            }
            $setmeal[$k]['configjson'] = $configjson_array;

        }
        $shop['setmeal'] = $setmeal;
        $shop['manystoretaps'] = Manystoretaps::where('id','in',$shop['manystoretaps_ids'])->field('id,name')->select();
        $shop['manystore'] = ManystoreShop::where('id','=',$shop['shop_id'])->field('id,name,logo,image,tel,address,content')->find();
        $shop['manystore']['logo'] = get_img_domain($shop['manystore']['logo'],false);
        $shop['manystore']['image'] = get_img_domain($shop['manystore']['image'],false);
        $this->success('获取成功',$shop);
    }
    /**
     * 获取商品评价列表
     *
     * @ApiTitle    (获取商品评价列表)
     * @ApiSummary  (获取商品评价列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/getArticleList)
     * @ApiParams  (name=shoplist_id, type=int, required=false, description="商品id")
     * @ApiParams   (name="page", type="integer", required=true, description="分页")
     * @ApiParams   (name="limit", type="integer", required=true, description="limit")
     * @ApiParams   (name="order", type="string", required=true, description="hot new")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="string", required=true, sample="数据")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function getArticleList(){
        $shoplist_id = $this->request->request("shoplist_id/d");
        $page = $this->request->request("page/d");
        $limit = $this->request->request("limit/d");
        $order = $this->request->request("order/s");
        $where=[];
        if ($shoplist_id){
            $where['shoplist_id'] = $shoplist_id;
        }
        $db_order='id desc';
        if ($order == 'hot'){
            $db_order='likenum desc';
        }








        $article = Article::alias('a')
            ->join('user w','a.user_id = w.id')
            ->order('a.id desc')
            ->where($where)
            ->page($page,$limit)
            ->field('a.id,a.shoplist_id,a.user_id,a.title,a.likenum,a.createtime,a.smallimages,a.content,w.nickname,w.avatar')
            ->order($db_order)
            ->select();
        if (count($article) == 0){

           return $this->error('暂时没有更多评论','');
        }


        //补全URL地址
        foreach ($article as $k=>$v){
            $value = explode(',', $v['smallimages']);
            if (is_array($value)){
                foreach ($value as $k1 => $v1) {
                    $value[$k1] = get_img_domain($v1,false);
                }
                $article[$k]['smallimages'] = implode($value, ',');
            }

            if (!$v['avatar']) {
                //如果不需要启用首字母头像，请使用
                $article[$k]['avatar'] = '/assets/img/avatar.png';
            }
            $article[$k]['avatar'] = get_img_domain($article[$k]['avatar'],false);
        }


//        //补全URL地址
//        foreach ($article as $k=>$v){
//            $value = explode(',', $v['smallimages']);
//            foreach ($value as $k1 => $v1) {
//                $value[$k1] = get_img($v1);
//            }
//            $article[$k]['smallimages'] = implode($value, ',');
//        }
        return  $this->success('查询成功',$article);
    }
    /**
     * 获取商品评价
     *
     * @ApiTitle    (获取商品评价)
     * @ApiSummary  (获取商品评价)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/getArticle)
     * @ApiParams  (name=shoplist_id, type=int, required=false, description="商品id")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="string", required=true, sample="数据")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function getArticle(){
        $shoplist_id = $this->request->request("shoplist_id/d");
        $where=[];
        if ($shoplist_id){
            $where['shoplist_id'] = $shoplist_id;
        }else{
            return $this->error('参数信息错误');
        }
        $article = Article::where($where)->field('*')->find();
        if (!$article){
            return  $this->error('没有更多内容了');
        }
        //补全URL
        $smallimages = explode(',', $article['smallimages']);
        foreach ($smallimages as $k1 => $v1) {
            $smallimages[$k1] = get_img_domain($v1,false);
        }
        $article['smallimages'] = implode($smallimages, ',');

        return  $this->success('查询成功',$article);
    }







}
